package com.hnzq.cloud.freshaddressassembly.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.api.SiteAreaApi;
import com.hnzq.cloud.areaParam.AddSiteAreaParam;
import com.hnzq.cloud.freshaddressassembly.domain.FreSiteArea;
import com.hnzq.cloud.freshaddressassembly.service.IFreSiteAreaService;
import com.hnzq.cloud.freshaddressassembly.util.WoniuBeanUtils;
import com.hnzq.cloud.model.Result;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qinle
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/freSiteArea")
public class FreSiteAreaController implements SiteAreaApi {
    @Resource
    private IFreSiteAreaService freSiteAreaService;
     //新增站点区域
    @PostMapping("/addSiteArea")
    @Override
    public Result addSiteArea(AddSiteAreaParam addSiteAreaParam) {
          //查出所有没有被删除的战点区域

        List<FreSiteArea> siteAreas = freSiteAreaService.list(new QueryWrapper<FreSiteArea>().eq("is_delete", 0));
        //System.out.println(siteAreas.size());
        for(int i=0;i<siteAreas.size();i++) {
             //如果新增站点区域离已有门店区域的距离小于他的半径距离，就不允许新增

             if ((WoniuBeanUtils.getNewDistance(Double.valueOf(addSiteAreaParam.getAreaX()), Double.valueOf(addSiteAreaParam.getAreaY()), Double.valueOf(siteAreas.get(i).getAreaX()), Double.valueOf(siteAreas.get(i).getAreaY()))-Double.valueOf(siteAreas.get(i).getAreaLength()))<0) {
                 //System.out.println("sss");
                 return Result.newInstance().setMessage("新增站点区域失败");
             }
         }
             if (freSiteAreaService.save(WoniuBeanUtils.copyObject(addSiteAreaParam, FreSiteArea.class))) {
                 //System.out.println("bbb");
                 return  Result.newInstance().setMessage("新增站点区域成功");
                 }else {
                 return  Result.newInstance().setMessage("新增站点区域失败");
             }

    }
     //删除站点区域
    @DeleteMapping("/deleteArea")
    @Override
    public Result deleteArea(Integer areaId) {
        Double newDistance = WoniuBeanUtils.getNewDistance(106.505542, 29.604264, 106.53279532319, 29.59154);
        System.out.println(newDistance);
        FreSiteArea siteArea = freSiteAreaService.getById(areaId);
        siteArea.setIsDelete(1);
        if (freSiteAreaService.updateById(siteArea)) {
            return Result.newInstance().setMessage("删除站点区域成功");
        }else {
            return Result.newInstance().setMessage("删除站点区域失败");
        }
    }
}

